#!/usr/bin/perl -w  # useful for syntax highlighting
package clreqd_modules;

use File::Basename;
use clutils qw(to_bool);

sub config_backup {
my %ARGS=@_;
my ($file,$fd);

	local $SIG{CHLD}='DEFAULT';
	my $overwrite=0;
	if(exists($ARGS{OVERWRITE}) && defined($ARGS{OVERWRITE})) {
		$overwrite=to_bool($ARGS{OVERWRITE});
	}
	$file=$ARGS{FILE};
	if(! $overwrite && -e $file) {
		return "FAIL File '$file' already exists.";
	}
	my $d=dirname($file);
	if(! -e $d) {
		return "FAIL Directory '$d' does not exist.";
	}
	if(! -d $d) {
		return "FAIL '$d' is not a directory.";
	}
	if(! -w $d) {
		return "FAIL Directory '$d' is not writable.";
	}

	if(-x "/bin/tar") {
		system("cd $::ENV{TRUECL_CFG}; /bin/tar cf $file . 2>/dev/null");
	} else {
		system("cd $::ENV{TRUECL_CFG}; /usr/bin/tar cf $file . 2>/dev/null");
	}
	if($?>>8) {
		return "FAIL Unable to create file '$file'.";
	}
	return "OK";
}

1;
